package com.test.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.test.table.Answer;
import com.test.table.Question;
import com.test.table.TrueAnswer;

public class Transformer {

	public List<String> transformQuestionsToString(List<Question> questions){
		List<String> questionList = new ArrayList<>();
		for(Question question : questions){		
			switch(question.getKind()){
			case "one"   : questionList.add(one(question));break;
			case "many"  : questionList.add(many(question));break;
			case "string": questionList.add(string(question));
			}
		}
		return questionList;		
	}
	
	public Map<String,Set<TrueAnswer>> transformResultToAnswers(Map<String,String[]> parameters){
		Map<String,Set<TrueAnswer>> answers = new HashMap<>();
		for(String s : parameters.keySet()){
			answers.put(s,createAnswers(parameters.get(s)));
		}
		return answers;
	}
	
	private Set<TrueAnswer> createAnswers(String[] parameter){
		HashSet<TrueAnswer> answers = new HashSet<>();
		for(String s : parameter){
			TrueAnswer answer = new TrueAnswer();
			answer.setTrueAnswer(s);
			answers.add(answer);
		}
		return answers;	
	}
		
	private String one(Question question){
		StringBuilder result = new StringBuilder();
		result.append(question.getQuestion()+"<br>");
		for(Answer answer : question.getAnswer()){
			result.append(" <input type='radio' name='"+question.getQuestion()+"' value='"+answer.getAnswer()+"' >"+answer.getAnswer()+"<br>");
		}
		return result.toString();
	}
	
	private String many(Question question){
		StringBuilder result = new StringBuilder();
		result.append(question.getQuestion()+"<br>");
		for(Answer answer : question.getAnswer()){
			result.append(" <input type='checkbox' name='"+question.getQuestion()+"' value='"+answer.getAnswer()+"'>"+answer.getAnswer()+"<br>");
		}
		return result.toString();
	}
	
	private String string(Question question){
		return question.getQuestion()+"<br><input type='text' name='"+question.getQuestion()+"'><br>";
	}

}
